package io.github.cubelitblade.librarymanagementbackend.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.github.cubelitblade.librarymanagementbackend.entity.User;
import io.github.cubelitblade.librarymanagementbackend.mapper.UserMapper;
import io.github.cubelitblade.librarymanagementbackend.vo.UserVO;
import org.springframework.stereotype.Service;

@Service
public class UserService extends ServiceImpl<UserMapper, User> {
    private final BorrowService borrowService;
    private final FavoriteService favoriteService;

    UserService(BorrowService borrowService, FavoriteService favoriteService) {
        this.borrowService = borrowService;
        this.favoriteService = favoriteService;
    }

    /**
     * 判断是否存在对应的用户。
     *
     * @param userId 用户ID。
     * @return 如果用户ID对应的用户存在，则返回 {@code true} ；否则返回 {@code false} 。
     */
    public boolean existsUser(Integer userId) {
        return this.getById(userId) != null;
    }

    public UserVO getUser(Integer userId) {
        User user = this.getById(userId);
        return UserVO.of(
                user,
                borrowService.countBorrows(userId),
                favoriteService.countFavorites(userId)
        );
    }
}
